﻿using DG.Tweening;
using UnityEngine;
using Yoozoo.UI.YGUI;

public class LuckTurntableEffect : MonoBehaviour
{
    public Transform turntableTrans;
    public Transform multiplierTrans;
    public YButton onceBtn;
    public YButton fiveBtn;

    public int onceLotteryNumberOfTurns = 3;
    public float onceLotteryRotateTime = 2f;
    public int fiveLotteryNumberOfTurns = 1;
    public float fiveLotteryRotateTime = 1f;

    private bool m_IsOnce;

    private int rewardCount;
    private int targetRewardIndex;
    private int currStayRewardIndex;

    private int multiplierCount;
    private int targetMultiplierIndex;
    private int currStayMultiplierIndex;

    private float circumferenceAngle = 360.0f;

    private float currTime;
    private float numberOfTurns;
    private float rotateTime;
    private bool isPlayingEffect;

    private float targetRewardAngle;
    private float targetRewardAngleVelocity;

    private float targetMultiplierAngle;
    private float targetMultiplierAngleVelocity;

    private float currRewardAngle;
    private float currMultiplierAngle;

    private int fiveLotteryCount;

    private int currInEffectMode;// -1 没有开始旋转 0 道具层旋转 1 倍率层旋转

    private void Start()
    {
        currTime = 0;
        rewardCount = 10;
        currStayRewardIndex = 1;
        multiplierCount = 10;
        currStayMultiplierIndex = 1;
        currInEffectMode = 0;
        onceBtn.onClick.AddListener(() =>
        {
            PlayEffect(true);
        });
        fiveBtn.onClick.AddListener(() =>
        {
            PlayEffect(false);
        });
    }
    private void PlayEffect(bool isOnce)
    {
        m_IsOnce = isOnce;
        if (m_IsOnce)
        {
            numberOfTurns = onceLotteryNumberOfTurns;
            rotateTime = onceLotteryRotateTime;
        }
        else
        {
            numberOfTurns = fiveLotteryNumberOfTurns;
            rotateTime = fiveLotteryRotateTime;
            fiveLotteryCount = 0;
        }
        CalcLottery();
        isPlayingEffect = true;
    }
    private void CalcLottery()
    {
        targetRewardIndex = Random.Range(1, 10);
        targetMultiplierIndex = Random.Range(1, 10);

        currRewardAngle = turntableTrans.eulerAngles.z;
        currMultiplierAngle = multiplierTrans.eulerAngles.z;

        targetRewardAngle = (targetRewardIndex - currStayRewardIndex) / (float)rewardCount * circumferenceAngle;
        if (targetRewardAngle < 0) targetRewardAngle += circumferenceAngle;
        targetRewardAngle += numberOfTurns * circumferenceAngle;
        targetRewardAngleVelocity = targetRewardAngle / rotateTime * 2;
        Debug.LogFormat("currStayRewardIndex:{0},targetRewardIndex:{1},currRewardAngle:{2},targetRewardAngle:{3},targetRewardAngleVelocity:{4}", currStayRewardIndex, targetRewardIndex, currRewardAngle, targetRewardAngle, targetRewardAngleVelocity);

        targetMultiplierAngle = (targetMultiplierIndex - currStayMultiplierIndex) / (float)multiplierCount * circumferenceAngle;
        if (targetMultiplierAngle < 0) targetMultiplierAngle += circumferenceAngle;
        targetMultiplierAngle += numberOfTurns * circumferenceAngle;
        targetMultiplierAngleVelocity = targetMultiplierAngle / rotateTime * 2;
        Debug.LogFormat("currStayMultiplierIndex:{0},targetMultiplierIndex:{1},currMultiplierAngle:{2},targetMultiplierAngle:{3},targetMultiplierAngleVelocity:{4}", currStayMultiplierIndex, targetMultiplierIndex, currMultiplierAngle, targetMultiplierAngle, targetMultiplierAngleVelocity);
    }
    private void PlayEffectFinish()
    {
        currTime = 0;
        currStayRewardIndex = targetRewardIndex;
        currStayMultiplierIndex = targetMultiplierIndex;
        if (m_IsOnce)
        {
            isPlayingEffect = false;
        }
        else
        {
            fiveLotteryCount++;
            if (fiveLotteryCount < 5)
            {
                CalcLottery();
            }
            else
            {
                isPlayingEffect = false;
            }
        }
    }
    private void Update()
    {
        if (!isPlayingEffect) return;
        currTime += Time.deltaTime;
        if (currInEffectMode == 0)
        {
            if (currRewardAngle >= targetRewardAngle || currTime >= rotateTime)
            {
                Debug.Log("Reward Rotate Finish");
                currTime = 0;
                currInEffectMode = 1;
                return;
            }
            float diffAngle = targetRewardAngleVelocity * currTime - (targetRewardAngleVelocity / rotateTime) * currTime * currTime / 2;
            turntableTrans.eulerAngles = new Vector3(0f, 0f, currRewardAngle - diffAngle);
        }
        else if (currInEffectMode == 1)
        {
            if (currMultiplierAngle >= targetMultiplierAngle || currTime >= rotateTime)
            {
                Debug.Log("Multiplier Rotate Finish");
                currInEffectMode = 0;
                PlayEffectFinish();
                return;
            }
            float diffAngle = targetMultiplierAngleVelocity * currTime - (targetMultiplierAngleVelocity / rotateTime) * currTime * currTime / 2;
            multiplierTrans.eulerAngles = new Vector3(0f, 0f, currMultiplierAngle + diffAngle);
        }
    }
}